<?php
/**
 * @desc 站点控制器
 * @author  tom_xia 
 */
require_once APPPATH."/libraries/Jredis.php";
class Site extends CI_Controller{
    private $userInfo = [
        "dxp"=>"123456",
//        "admin"=>"Qmjs_admin@177.com",
        "admin"=>"Host_admin@177.com",
        "yunying01"=>"Qmjs_yunying01@177.com",
        "yunying02"=>"Qmjs_yunying02@177.com",
        "yunying03"=>"Qmjs_yunying03@177.com",
        "shenhe01"=>"Qmjs_shenhe01@177.com",
        "shenhe02"=>"Qmjs_shenhe02@177.com",
        "shenhe03"=>"Qmjs_shenhe03@177.com",
        "shenhe04"=>"Qmjs_shenhe04@177.com",
        "shenhe05"=>"Qmjs_shenhe05@177.com",
        "shenhe06"=>"Qmjs_shenhe06@177.com",
        "shenhe07"=>"Qmjs_shenhe07@177.com",
        "shenhe08"=>"Qmjs_shenhe08@177.com",
        "shenhe09"=>"Qmjs_shenhe09@177.com",
        "shenhe10"=>"Qmjs_shenhe10@177.com",
    ];
    private $priv = [
        "dxp"=>1,
        "admin"=>1,
        "yunying01"=>2,
        "yunying02"=>2,
        "yunying03"=>2,
        "shenhe01"=>3,
        "shenhe02"=>3,
        "shenhe03"=>3,
        "shenhe04"=>3,
        "shenhe05"=>3,
        "shenhe06"=>3,
        "shenhe07"=>3,
        "shenhe08"=>3,
        "shenhe09"=>3,
        "shenhe10"=>3,
    ];
	public function __construct()
	{
		parent::__construct();
		$this->load->helper("url");
		$this->load->helper("common");
		$this->load->helper("cookie");
	}
	public function index()
	{
        $data["urls"] = [
	        "login"=>base_url("site/login"),
        ];
	    $this->load->view("index",$data);
	}
    /**
     * @desc用户登录
     */
	public function login()
    {
        $userName = trim($this->input->post("user_name"));
        $password = trim($this->input->post("password"));

        //1.验证字段是否合法
        if(empty(trim($userName))){

            response(-1,"用户名不能为空");
        }
        if(empty(trim($password))){
            response(-1,"密码不能为空");
        }
        //2.验证账户和密码是否正确
        if(!array_key_exists($userName,$this->userInfo)){
            response(-1,"用户名错误");

        }
//        $verityPassword = md5(substr(md5("admin123456@2018"),0,30));
        $verityPassword = md5(substr(md5($this->userInfo["$userName"]),0,30));
        $inputPassword = md5(substr(md5($password),0,30));
        if($inputPassword != $verityPassword){
            response(-1,"密码错误");
        }
        $userInfo["name"] = $userName;
        $userInfo["role_id"] = $this->priv[$userName];
        $userInfo["sid"] = intval(microtime(true));
        set_cookie("user_info",auth_code(serialize($userInfo),"ENCODE"),time()+JPER_CMS_REDIS_USERINFO_TIMEOUT);
        if($userInfo["role_id"] == 1 || $userInfo["role_id"] == 3){
            response(0,"登录成功",["location"=>base_url("home/index")]);
        }else{
            response(0,"登录成功",["location"=>base_url("home/index")]);

        }
    }
}
